package com.soft.mainsite.component.beetl;

import com.soft.mainsite.common.utils.CmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.ConsoleErrorHandler;
import org.beetl.core.exception.BeetlException;
import org.beetl.core.exception.ErrorInfo;

import java.io.IOException;
import java.io.Writer;

/**
 * Description:BeetlWebErrorHandler
 *
 * @author Jin
 * @create 2017-06-03
 **/
@Slf4j
public class BeetlWebErrorHandler extends ConsoleErrorHandler {

    @Override
    public void processExcption(BeetlException ex, Writer writer) {
        ErrorInfo err = new ErrorInfo(ex);
        StringBuffer content = new StringBuffer();
        content.append("<!DOCTYPE html>");
        content.append("<html>");
        content.append("<head>");
        content.append("<meta charset=\"UTF-8\">");
        content.append("<title>模板解析错误</title>");
        content.append("</head>");
        content.append("<body>");
        content.append("<p style=\"font-size:22px;padding-left:10px;\"><b>" + err.getType() + "</b></p>");
        content.append("<p style=\"font-size:18px;padding-left:10px;\">描述 : " + (!CmsUtil.isNullOrEmpty(err.getMsg()) ? err.getMsg() : "上面信息已经很详细 :)") + (CmsUtil.isNullOrEmpty(err.getCause()) ? "" : "( " + err.getCause() + " )") + " </p>");
        content.append("<p style=\"font-size:18px;padding-left:10px;\">位置 : " + err.getResourceCallStack() + " ~ 第" + err.getErrorTokenLine() + "行</p>");
        content.append("</body>");
        content.append("</html>");
        try {
            writer.write(content.toString());
        } catch (IOException e) {
            log.error("模板渲染失败", e);
        }
    }

}
